package com.theory.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.theory.mapper.QuestionMapper;
import com.theory.pojo.Question;
import com.theory.service.QuestionService;
import com.theory.util.HttpClientUtils;
import com.theory.util.HttpExecuteResponse;
import com.theory.util.MyProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.HashMap;

@Service
@Slf4j
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements QuestionService {


    @Override
    public void insertQuestion(Question q) {
        baseMapper.insert(q);
    }

    @Override
    public IPage<Question> listPage(Page<Question> pageParam) {
        return baseMapper.selectPage(pageParam, null);
    }

    @Override
    public void jsonToBean(String fileName) {
        String path = QuestionServiceImpl.class.getClassLoader().getResource(fileName).getPath();
        String s = readJsonFile(path);
        JSONObject jsonObject = JSON.parseObject(s);
        JSONObject dd = jsonObject.getJSONObject("d");
        JSONObject data = dd.getJSONObject("Data");
        JSONArray question = data.getJSONArray("Question");


        for (int i = 0; i < question.size(); i++) {
            JSONObject item = (JSONObject) question.get(i);
            Integer no = (Integer) item.get("No");
            String topic = (String) item.get("Topic");
            topic = MyProperties.URL + topic;
            String a = (String) item.get("A");
            String b = (String) item.get("B");
            String c = (String) item.get("C");
            String d = (String) item.get("D");

            Question q ;


            if (i < MyProperties.LIMIT) {
                String ocr = null;
                try {
                    ocr = ocr(topic);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                q = new Question(no, topic, a, b, c, d, ocr);
            } else {
                q = new Question(no, topic, a, b, c, d, null);
            }
            insertQuestion(q);
        }
    }

    @Override
    public String ocr(String imgUrl) {
        String url = "https://ocrapi-advanced.taobao.com/ocrservice/advanced";
        HashMap<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + MyProperties.APPCODE);

        headers.put("Content-Type", "application/json; charset=UTF-8");
        String bodys = "{\"img\":\"\",\"url\":" + "\"" + imgUrl + "\"" + ",\"prob\":false,\"charInfo\":false,\"rotate\":false,\"table\":false}";
        JSONObject jsonObject = null;
        try {
            HttpExecuteResponse response = HttpClientUtils.doPost(url, bodys, headers);
            String responseAsString = response.getResponseAsString();
            jsonObject = JSON.parseObject(responseAsString);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject.getString("content");
    }

    @Override
    public String readJsonFile(String fileName) {
        String jsonStr = "";
        try {
            File jsonFile = new File(fileName);
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }


}
